//
//  OpenFilePlugin.swift
//  Runner
//
//  Created by FancyLou on 2023/5/23.
//

import UIKit
import Flutter
import QuickLook


class OpenFilePlugin {
    //预览文件
    private lazy var previewVC: FilePreviewController = {
        return FilePreviewController()
    }()
    
    //
    func handleMethodCallOpenFile(filePath: String, result: @escaping FlutterResult) {
        let currentURL = NSURL(fileURLWithPath: filePath)
        if QLPreviewController.canPreview(currentURL) {
            self.previewVC.currentFileURLS.removeAll()
            self.previewVC.currentFileURLS.append(currentURL)
            self.previewVC.reloadData()
            guard let root = (UIApplication.shared.delegate as? AppDelegate)?.window.rootViewController else {
                result("fail")
                return
            }
            root.show(self.previewVC, sender: nil)
            result("success")
        } else {
            //self.showError(title: "当前文件类型不支持预览！")
            result("fail")
        }
         
    }
    
}
